//
//  JJChatBarActionsView.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/12.
//

import UIKit
import RxSwift

//上限127个按钮.如果需要更多修改类型.仅目前来讲应该够用才对
typealias Count_type = Int8
var chatBarActionsViewHeight : CGFloat = 230
class JJChatBarActionsView: UIView {
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    let actions : [IMChatBarAction] = {
        
        var optionActions = [
            IMChatBarAction(id: 0, title: "相册", iconName: "xxx"),
            IMChatBarAction(id: 1, title: "相机", iconName: "xxx"),
            IMChatBarAction(id: 2, title: "音视频", iconName: "xxx"),
            IMChatBarAction(id: 3, title: "位置", iconName: "xxx"),
            IMChatBarAction(id: 4, title: "文件", iconName: "xxx"),
            IMChatBarAction(id: 5, title: "名片", iconName: "xxx"),
            IMChatBarAction(id: 0, title: "相册1", iconName: "xxx"),
            IMChatBarAction(id: 1, title: "相机1", iconName: "xxx"),
            IMChatBarAction(id: 2, title: "音视频1", iconName: "xxx"),
            IMChatBarAction(id: 3, title: "位置1", iconName: "xxx"),
            IMChatBarAction(id: 4, title: "文件1", iconName: "xxx"),
            IMChatBarAction(id: 5, title: "名片1", iconName: "xxx"),
        ]
        //不足8的倍数则填充空白
        let emptyCount = 8 - (optionActions.count % 8)
        for i in 0 ..< emptyCount {
            optionActions.append(IMChatBarAction(id: -2, title: "", iconName: ""))
        }
        //扭转顺序
        var split = [[IMChatBarAction]]()
        while optionActions.count > 0 {
            let prefix8 = Array(optionActions.prefix(8))
            split.append(prefix8)
            for i in 0 ..< 8 {
                if optionActions.count > 0 {
                    optionActions.removeFirst()
                }else{
                    break
                }
            }
        }
        var list = [IMChatBarAction]()
        var divide0 = [IMChatBarAction]()
        var divide1 = [IMChatBarAction]()
        for subList in split {
            divide0.removeAll()
            divide1.removeAll()
            for i in 0 ..< subList.count {
                if i < 4 {
                    divide0.append(subList[i])
                }else{
                    divide1.append(subList[i])
                }
            }
            for i in 0 ..< 4 {
                list.append(divide0[i])
                list.append(divide1[i])
            }
        }
        return list
    }()

    let collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSizeMake(screen_width() / 4, (chatBarActionsViewHeight - safearea_bottom()) / 2)
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.register(IMChatBarAcitonItem.classForCoder(), forCellWithReuseIdentifier: "IMChatBarAcitonItem")
        collectionView.isPagingEnabled = true
        return collectionView
    }()
    
    let rx_selectedItem = PublishSubject<Count_type>()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        config()
    }
    func config(){
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.bottom.equalTo(-safearea_bottom())
            make.right.equalTo(0)
        }
        collectionView.delegate = self
        collectionView.dataSource = self
    }
}

extension JJChatBarActionsView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.actions.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: "IMChatBarAcitonItem", for: indexPath) as! IMChatBarAcitonItem
        item.bindFromModel(model: self.actions[indexPath.row])
        return item
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath)
        print(self.actions[indexPath.row].id)
        print(self.actions[indexPath.row].title)
        if self.actions[indexPath.row].id > -1 {
            self.rx_selectedItem.onNext(self.actions[indexPath.row].id)
        }
        
    }
}



class IMChatBarAction {
    //上限127个按钮.如果需要更多修改类型.仅目前来讲应该够用才对
    var id : Count_type = 0
    var title = ""
    var iconName = ""
    init(id: Count_type = 0, title: String = "", iconName: String = "") {
        self.id = id
        self.title = title
        self.iconName = iconName
    }
}

class IMChatBarAcitonItem : UICollectionViewCell {
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    let titleLabel = UILabel()
    let iconIV = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.config()
    }
    
    func config(){
        self.contentView.addSubview(titleLabel)
        self.contentView.addSubview(iconIV)
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(0)
            make.height.equalTo(20)
        }
        iconIV.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(80)
        }
    }
    
    func bindFromModel(model : IMChatBarAction) {
        if model.id < 0 {
            self.titleLabel.isHidden = true
            self.iconIV.isHidden = true
        }else{
            self.titleLabel.isHidden = false
            self.iconIV.isHidden = false
            self.titleLabel.text = model.title
            self.iconIV.image = UIImage(named: model.iconName)
        }
    }
    
    
}

